<?php

include_once '../lib/facebook.php';
include_once '../includes/config.php';
include_once '../includes/dbconnect.php';
include_once '../includes/function.php';
include_once '../tableObjects/TblMember.php';
include_once '../services/MemberService.php';

$member_service = new MemberService();

if (SANDBOX_MODE == 0) {
    try {
        if (isset($_REQUEST['request_ids'])) {
            echo "<script type='text/javascript'>top.location.href = '" . FACEBOOK_PAGE_TAB_URL . "';</script>";
            exit;
        }
        
        $loginUrl = $facebook->getLoginUrl(
                array(
                    'canvas' => 1,
                    'fbconnect' => 0,
                    'redirect_uri' => FACEBOOK_CANVAS_URL."/index.php?from_mobile=1",                      
                    'scope' => 'email,publish_stream,user_location,user_birthday'
                )
        );

        if (!$session) {
            //echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
            echo FACEBOOK_CANVAS_URL."/index.php?from_mobile=1";
            //echo "<script type='text/javascript'>top.location.href = '".FACEBOOK_CANVAS_URL."/index.php?from_mobile=1';</script>";
            exit;
        } else {
            try {
                /* ======================  User is Authorized ========================== 
                  fans process start */
                $uid = $facebook->getUser();
                $me = $facebook->api('/me');


                $get_member = $member_service->getMember(" where fb_id='$uid' ");

                if (!$get_member) {

                    //echo "insert into tbl_contestant (con_name,fb_id,email,created_datetime,status)values('".mysql_real_escape_string($me['name'])."','$uid','".$me['email']."',now(),'1')";
                    $created_datetime = date("Y-m-d H:i:s");

                    $tbl_member = new TblMember();
                    $tbl_member->setDob(convertDate($me['birthday'], 'Y-m-d'));
                    $tbl_member->setEmail($me['email']);
                    $tbl_member->setFb_id($uid);
                    $tbl_member->setMember_name(mysql_real_escape_string($me['name']));
                    $tbl_member->setCreated_datetime($created_datetime);

                    $member_id = $member_service->insertMember($tbl_member);

                    $_SESSION['login_id'] = $member_id;
                    $_SESSION['fb_id'] = $uid;
                    $_SESSION['login_name'] = $me['name'];
                    $_SESSION['registration_status'] = "0";
                } else {
                    $obj = $get_member[0];
                    $tbl_member = new TblMember();
                    $tbl_member->setDob($me['birthday']);
                    $tbl_member->setEmail($me['email']);
                    $tbl_member->setFb_id($uid);
                    $tbl_member->setMember_name($me['name']);
                    $tbl_member->setId($obj->getId());

                    $_SESSION['login_id'] = $obj->getId();
                    $_SESSION['fb_id'] = $uid;
                    $_SESSION['login_name'] = $me['name'];
                    $_SESSION['registration_status'] = $obj->getRegistration_status();
                }


                echo "<script type='text/javascript'>window.location='landing.php';</script>";
            } catch (FacebookApiException $e) {
                echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";

                exit;
            }
        }
    } catch (FacebookApiException $e) {

        echo "Error:" . print_r($e, true);
    }
} else {
    if (!empty($_SESSION['user_obj'])) {
        unset($_SESSION['user_obj']);
    }

    $fb_id = "677487661";

    $get_member = $member_service->getMember(" where fb_id='$fb_id' ");
    $obj = $get_member[0];

    $_SESSION['login_id'] = $obj->getId();
    $_SESSION['fb_id'] = $fb_id;
    
    $_SESSION['login_name'] = $obj->getMember_name();
    $_SESSION['registration_status'] = $obj->getRegistration_status();

    echo "<script type='text/javascript'>window.location='landing.php';</script>";
}
?>
